/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui;

import com.googlecode.jaden.common.other.Utils;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class Actions {

    private final UI ui;
    private final Action openProjectAction;
    private final Action newManualAction;
    private final Action newAutomaticAction;
    private final Action clearReopenAction;
    private final Action saveProjectAction;
    private final Action saveProjectAsAction;
    private final Action preferencesAction;
    private final Action closeProjectAction;
    private final Action editProjectDetailsAction;
    private final Action editProjectDefinitionAction;
    private final Action editTrainingFilesAction;
    private final Action editFileMappingsAction;
    private final Action manualTrainingAction;
    private final Action automatedTrainingAction;
    private final Action aboutAction;

    private final Action exitAction;

    private boolean haveProject;
    private boolean projectModified;

    public Actions(UI ui) {
        this.ui = ui;
        openProjectAction = createOpenProjectAction();
        newManualAction = createNewManualAction();
        newAutomaticAction = createNewAutomaticAction();
        clearReopenAction = createClearReopenAction();
        closeProjectAction = createCloseProjectAction();
        saveProjectAction = createSaveProjectAction();
        saveProjectAsAction = createSaveProjectAsAction();
        preferencesAction = createPreferencesAction();
        exitAction = createExitAction();
        editProjectDetailsAction = createEditProjectDetailsAction();
        editProjectDefinitionAction = createEditProjectDefinitionAction();
        editTrainingFilesAction = createEditTrainingFilesAction();
        editFileMappingsAction = createEditFileMappingsAction();
        manualTrainingAction = createManualTrainingAction();
        automatedTrainingAction = createAutomatedTrainingAction();
        aboutAction = createAboutAction();
        setHaveProject(false);
        setProjectModified(false);
    }

    private Action createAboutAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_ABOUT_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doAbout();
            }
        };
    }

    private Action createManualTrainingAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_MANUAL_TRAINING_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doManualTraining();
            }
        };
    }

    private Action createAutomatedTrainingAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_AUTOMATED_TRAINING_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.configureAutomatedTraining();
            }
        };
    }

    private Action createEditFileMappingsAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_EDIT_FILE_MAPPINGS_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doEditFileMappings();
            }
        };
    }

    private Action createNewManualAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_NEW_MANUAL_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doNewManual();
            }
        };
    }

    private Action createNewAutomaticAction() {
        AbstractAction action = new AbstractAction(Utils.translate(Constants.ACTION_NEW_AUTOMATIC_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doNewAutomatic();
            }
        };
        action.putValue(Action.ACCELERATOR_KEY, Utils.getKeyStroke(Utils.translate(Constants.ACTION_NEW_AUTOMATIC_ACC)));
        return action;
    }

    public Action getManualTrainingAction() {
        return manualTrainingAction;
    }

    public Action getAutomatedTrainingAction() {
        return automatedTrainingAction;
    }

    public Action getEditFileMappingsAction() {
        return editFileMappingsAction;
    }

    public Action getOpenProjectAction() {
        return openProjectAction;
    }

    public Action getCloseProjectAction() {
        return closeProjectAction;
    }

    public Action getExitAction() {
        return exitAction;
    }

    public Action getEditProjectDetailsAction() {
        return editProjectDetailsAction;
    }

    public Action getPreferencesAction() {
        return preferencesAction;
    }

    public Action getSaveProjectAsAction() {
        return saveProjectAsAction;
    }

    public Action getSaveProjectAction() {
        return saveProjectAction;
    }

    public Action getEditTrainingFilesAction() {
        return editTrainingFilesAction;
    }

    public Action getEditProjectDefinitionAction() {
        return editProjectDefinitionAction;
    }

    public Action getAboutAction() {
        return aboutAction;
    }

    private Action createEditProjectDetailsAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_EDIT_PROJECT_DETAILS_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doEditProjectDetails();
            }
        };
    }

    private Action createEditProjectDefinitionAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_EDIT_PROJECT_DEFINITION_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doEditProjectDefinition();
            }
        };
    }

    private Action createEditTrainingFilesAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_EDIT_TRAINING_FILES_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doEditTrainingFiles();
            }
        };
    }

    private Action createOpenProjectAction() {
        AbstractAction action = new AbstractAction(Utils.translate(Constants.ACTION_OPEN_PROJECT_NAME),
                UiUtils.getImageIcon("images/open.png")) {
            public void actionPerformed(ActionEvent e) {
                ui.doOpenProject();
            }
        };
        action.putValue(Action.ACCELERATOR_KEY, Utils.getKeyStroke(Utils.translate(Constants.ACTION_OPEN_PROJECT_ACC)));
        return action;
    }

    private Action createClearReopenAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_CLEAR_REOPEN_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doClearReopen();
            }
        };
    }

    private Action createCloseProjectAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_CLOSE_PROJECT_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doCloseProject();
            }
        };
    }

    private Action createSaveProjectAction() {
        AbstractAction action = new AbstractAction(Utils.translate(Constants.ACTION_SAVE_PROJECT_NAME),
                UiUtils.getImageIcon("images/save.png")) {
            public void actionPerformed(ActionEvent e) {
                ui.doSaveProject();
            }
        };
        action.putValue(Action.ACCELERATOR_KEY, Utils.getKeyStroke(Utils.translate(Constants.ACTION_SAVE_PROJECT_ACC)));
        return action;
    }

    private Action createPreferencesAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_PREFERENCES_NAME),
                UiUtils.getImageIcon("images/preferences.png")) {
            public void actionPerformed(ActionEvent e) {
                ui.doPreferences();
            }
        };
    }

    private Action createSaveProjectAsAction() {
        Action action = new AbstractAction(Utils.translate(Constants.ACTION_SAVE_PROJECT_AS_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.doSaveProjectAs();
            }
        };
        action.putValue(Action.ACCELERATOR_KEY, Utils.getKeyStroke(Utils.translate(Constants.ACTION_SAVE_PROJECT_AS_ACC)));
        return action;
    }

    private Action createExitAction() {
        return new AbstractAction(Utils.translate(Constants.ACTION_EXIT_NAME)) {
            public void actionPerformed(ActionEvent e) {
                ui.tryToExit();
            }
        };
    }

    public void setHaveProject(boolean haveProject) {
        this.haveProject = haveProject;
        closeProjectAction.setEnabled(haveProject);
        saveProjectAction.setEnabled(projectModified && haveProject);
        saveProjectAsAction.setEnabled(haveProject);
        editProjectDetailsAction.setEnabled(haveProject);
        editProjectDefinitionAction.setEnabled(haveProject);
        editTrainingFilesAction.setEnabled(haveProject);
        editFileMappingsAction.setEnabled(haveProject);
        manualTrainingAction.setEnabled(haveProject);
        automatedTrainingAction.setEnabled(haveProject);
    }

    public void setProjectModified(boolean projectModified) {
        this.projectModified = projectModified;
        saveProjectAction.setEnabled(projectModified && haveProject);
    }

    public Action getClearReopenAction() {
        return clearReopenAction;
    }

    public Action getNewManual() {
        return newManualAction;
    }

    public Action getNewAutomatic() {
        return newAutomaticAction;
    }
}
